import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
 
 
class CellImageSize(QWidget):
    def __init__(self):
        super(CellImageSize, self).__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('在表格中显示上下文菜单')
        self.resize(500, 200)
 
        layout = QHBoxLayout()
        #表格对象
        self.tableWidget=QTableWidget()
        #五行三列
        self.tableWidget.setRowCount(4)
        self.tableWidget.setColumnCount(3)
        #设置表格字段
        self.tableWidget.setHorizontalHeaderLabels(['name','sex','weigh'])
 
        #添加数据
        newItem = QTableWidgetItem('小明')
        self.tableWidget.setItem(0, 0, newItem)
        newItem = QTableWidgetItem('男')
        self.tableWidget.setItem(0, 1, newItem)
        newItem = QTableWidgetItem('190')
        self.tableWidget.setItem(0, 2, newItem)
 
        newItem = QTableWidgetItem('小亮')
        self.tableWidget.setItem(1, 0, newItem)
        newItem = QTableWidgetItem('男')
        self.tableWidget.setItem(1, 1, newItem)
        newItem = QTableWidgetItem('100')
        self.tableWidget.setItem(1, 2, newItem)
 
        newItem = QTableWidgetItem('小红')
        self.tableWidget.setItem(2, 0, newItem)
        newItem = QTableWidgetItem('女')
        self.tableWidget.setItem(2, 1, newItem)
        newItem = QTableWidgetItem('90')
        self.tableWidget.setItem(2, 2, newItem)
 
        #设置允许弹出上下文你菜单
        self.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        #信号与槽
        self.tableWidget.customContextMenuRequested.connect(self.generateMenu)
 
        layout.addWidget(self.tableWidget)
        self.setLayout(layout)
 
    def generateMenu(self,pos):
        print('pos=',pos)
        for i in self.tableWidget.selectionModel().selection().indexes():
            rowNum=i.row()
            print('选择了第%d行' % (rowNum+1))
        #如果选择的行索引小于2，弹出上下文菜单
        if rowNum<2:
            menu=QMenu()
            item1=menu.addAction('菜单项1')#一个动作
            #print(type(item1))
            item2 = menu.addAction('菜单项2')
            item3 = menu.addAction('菜单项3')
            #pos是相对于整个屏幕的坐标，所以要转换为窗口坐标
            screenPos=self.tableWidget.mapToGlobal(pos)
            print('screenPos=',screenPos)
            #被阻塞
            action=menu.exec_(screenPos)
            if action==item1:
                print('选择了第一个菜单项',self.tableWidget.item(rowNum,0).text(),
                      self.tableWidget.item(rowNum, 1).text(),
                      self.tableWidget.item(rowNum, 2).text())
            elif action==item2:
                print('选择了第二个菜单项',self.tableWidget.item(rowNum,0).text(),
                      self.tableWidget.item(rowNum, 1).text(),
                      self.tableWidget.item(rowNum, 2).text())
            elif action==item3:
                print('选择了第三个菜单项',self.tableWidget.item(rowNum,0).text(),
                      self.tableWidget.item(rowNum, 1).text(),
                      self.tableWidget.item(rowNum, 2).text())
            else:
                return
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=CellImageSize()
    main.show()
    sys.exit(app.exec_())
 
